<template>
  <el-dialog title="授权" :visible.sync="dialogFormVisible">
    <el-form label-width="120px">
      <el-form-item label="授权品牌">
        <el-col :span="19">
          <el-select v-model="brand" multiple placeholder="请选择授权品牌" @change="selectVal">
            <el-option
              v-for="item in trees"
              :key="item.id"
              :label="item.brand_name"
              :value="item.id"
              :disabled="item.disabled"
            />
          </el-select>
        </el-col>

      </el-form-item>
      <el-form-item v-for="(item, index) in levelList" :key="index" :label="item.brand_name">
        <el-col :span="9">
          <el-select v-model="item.level" @change="selectLevel">
            <el-option
              v-for="item1 in item.level_content"
              :key="item1.level_int"
              :label="item1.level_name"
              :value="item1.level_int"
            >
              <span style="float: left;color: #8492a6;">{{ item1.level_int }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item1.level_name }}</span>
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="9" :offset="1">
          <el-input v-model="item.anent_fid" placeholder="上级代理编码" />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitBtn">提交</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getAgentTrees } from '@/api/agent'
export default {
  props: {
    levels: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      trees: [],
      brand: [],
      levelList: []
    }
  },
  created() {
    this.getAgentLevel()
  },
  methods: {
    // 获取所有授权品牌
    getAgentLevel() {
      getAgentTrees().then(res => {
        console.log('1', res)
        console.log('2', this.levels)
        if (res.length !== 0) {
          for (const i in this.levels) {
            for (const j in res) {
              if (this.levels[i].brand_id === res[j].id) {
                res[j].disabled = true
              }
            }
          }
        }
        this.trees = res
        console.log(this.trees)
      })
    },
    selectVal(val) {
      const trees = this.trees
      this.levelList = []
      for (const i in val) {
        for (const j in trees) {
          if (val[i] === trees[j].id) {
            this.levelList.push(trees[j])
          }
        }
      }
      console.log(this.levelList)
    },
    selectLevel(val) {

    },
    submitBtn() {
      this.$emit('selectLevels', this.levelList)
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
 .el-select {
    width: 100%;
  }
</style>
